import Cookie from 'js-cookie';

/**
 * 是否登录
 * @returns
 */
export function isLogin() {
  return !!Cookie.get('business');
}

/**
 * 删除某个cookie
 * @param name
 * @returns
 */
export function clearCookie(name: string) {
  return Cookie.remove(name);
}

/**
 * 设置cookie
 * @param name
 * @param value
 */
export function setCookie(name: string, value: any) {
  Cookie.set(name, value);
}

/**
 * 获取cookie
 * @param name
 * @returns
 */
export function getCookie(name: string) {
  return Cookie.get(name);
}

/**
 * 倒计时组件
 * @param {*} func 倒计时执行的函数
 * @param {*} count 倒计时时间
 */
export function countDown(func: (count: number) => void, count: number) {
  let interval: any;
  if (count > 0) {
    func(count);
    interval = setInterval(() => {
      count--;
      if (count == 0) {
        clearInterval(interval);
      }
      func(count);
    }, 1000);
  }
  return function () {
    count = 0;
    clearInterval(interval);
    func(count);
  };
}
